<template>
  <div class='sub-category'>
    <div class="container">
 <!-- 面包屑 -->
      <SubBread />
      <!-- 筛选区 -->
      <SubFilter @shaixuan="doshaixuan" />
      <div class="goods-list">
        <!-- 排序 -->
        <SubSort @xinsort="doxinsort" />
        <!-- 列表 -->
         <ul>
          <li v-for="item in list" :key="item.id" >
            <GoodsItem :goods="item" />
          </li>
        </ul>
          <!-- 加载 -->
        <XtxInfiniteLoading :loading="loading" :finished="finished" @load="onLoad" />
      </div>
      </div>
  </div>
</template>
<script>
import SubBread from './components/sub-bread'
import SubFilter from './components/sub-filter.vue'
import SubSort from './components/sub-sort'
import GoodsItem from './components/goods-item.vue'
import { findSubCategoryGoods } from '@/api/category.js'
import { ref, reactive, watch } from 'vue'
import { useRoute } from 'vue-router'
export default {
  name: 'SubCategory',
  components: {
    SubBread,
    SubFilter,
    SubSort,
    GoodsItem
  },
  setup () {
    // 获取筛选条件参数方法
    const doshaixuan = (sort) => {
      Object.assign(filterParams, sort)
      list.value = []
      total.value = 0
      loading.value = false
      finished.value = false
      filterParams.page = 1
    }
    const doxinsort = (sort) => {
      Object.assign(filterParams, sort)
      console.log(filterParams)
      if (list.value.length === 0) {
        onLoad()
      } else {
        // 重置数据即可(只要数据置空，加载更多的组件就会进入可视区，那么就触发load数据)
        list.value = []
      }

      total.value = 0
      loading.value = false
      finished.value = false
      filterParams.page = 1
    }
    const route = useRoute()
    // 获取筛选条件的数据
    const hobby = ref(true)
    // 商品列表
    const list = ref([])
    // 列表总数
    const total = ref(0)
    // 加载状态
    const loading = ref(false)
    const finished = ref(false)
    // 请求参数
    const filterParams = reactive({
      categoryId: route.params.id,
      page: 1,
      pageSize: 10
    })
    const onLoad = () => {
      // 刚加载显示正在加载
      loading.value = true
      findSubCategoryGoods(filterParams).then(res => {
        list.value.push(...res.result.items)
        // 总商品数据
        total.value = res.result.counts
        // 加载一次分页加1
        filterParams.page += 1
        if (list.value.length >= total.value) {
          finished.value = true
        }
        // 加载完显示没有
        loading.value = false
      })
    }
    // 如果二级分类发生变化，重置当前的数据
    watch(() => route.params.id, (newVal) => {
      // 判断是否为二级分类
      if (route.fullPath !== '/category/sub/' + newVal) return
      // 重置数据
      list.value = []
      total.value = 0
      loading.value = false
      finished.value = false
      filterParams.page = 1
      filterParams.categoryId = newVal
    })
    return { hobby, onLoad, list, loading, finished, doxinsort, doshaixuan }
  }
}
</script>
<style scoped lang='less'>
.goods-list {
  background: #fff;
  padding: 0 25px;
  margin-top: 25px;
  ul {
    display: flex;
    flex-wrap: wrap;
    padding: 0 5px;
    li {
      margin-right: 20px;
      margin-bottom: 20px;
      &:nth-child(5n) {
        margin-right: 0;
      }
    }
  }
}
</style>
